package com.cxyd.modules.wcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxyd.common.exception.RenException;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.modules.wcs.dao.StnMappingDao;
import com.cxyd.modules.wcs.dao.StockDao;
import com.cxyd.modules.wcs.dto.StnMappingDTO;
import com.cxyd.modules.wcs.entity.StnMappingEntity;
import com.cxyd.modules.wcs.entity.StockEntity;
import com.cxyd.modules.wcs.service.StnMappingService;
import com.cxyd.modules.wcs.service.StockService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StnMappingServiceImpl extends CrudServiceImpl<StnMappingDao, StnMappingEntity, StnMappingDTO> implements StnMappingService {
    @Resource
    private StockService stockService;
    @Resource
    private StockDao stockDao;

    @Override
    public QueryWrapper<StnMappingEntity> getWrapper(Map<String, Object> params) {
        String wcsStation = (String) params.get("wcsStation");
        String stationType = (String) params.get("stationType");
        String contNo = (String) params.get("contNo");
        String status = (String) params.get("status");
        final QueryWrapper<StnMappingEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(wcsStation), "wcs_station", wcsStation);
        wrapper.eq(StringUtils.isNotBlank(stationType), "station_type", stationType);
        wrapper.eq(StringUtils.isNotBlank(status), "status", status);
        wrapper.like(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        return wrapper;
    }

    @Override
    public void generateStnMapping(int row, int column, int level) {
        for (int r = 1; r <= row; r++) {
            for (int c = 1; c <= column; c++) {
                for (int l = 1; l <= level; l++) {
                    StnMappingEntity map = new StnMappingEntity();
                    map.setWmsStation(String.format("HW-01K%02d%02d%02d", r, c, l));
                    map.setWcsStation(String.format("HW-01K%02d%02d%02d", r, c, l));
                    baseDao.insert(map);
                }
            }
        }
    }

    @Override
    public StnMappingEntity getStnByNo(String StnNo) {
        QueryWrapper<StnMappingEntity> qw = new QueryWrapper<>();
        qw.eq("wcs_station", StnNo).last("limit 1");
        return baseDao.selectOne(qw);
    }

    @Override
    public void stnMappingDisabled(Map<String, Object> params) {
        Long id = Long.parseLong(params.get("id").toString());
        StnMappingEntity stnMappingEntity = baseDao.selectById(id);
        if (ObjectUtils.isEmpty(stnMappingEntity)) {
            throw new RenException("货位不存在");
        }
        Integer active = stnMappingEntity.getActive();
        int activeNew;
        if (0 == active) {
            activeNew = 1;
        } else {
            activeNew = 0;
        }
        StnMappingEntity updateLocate = new StnMappingEntity();
        updateLocate.setId(id);
        updateLocate.setActive(activeNew);
        baseDao.updateById(updateLocate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStnMapping(StnMappingDTO dto) {
        //删除或添加货位信息时，也须将库存一并修改 0-修改为无货 2-修改为有货
        if (dto.getStatus() == 0 && !dto.getStationType().equals("ST")) {
            List<StockEntity> stockEntities = stockDao.selectList(new QueryWrapper<StockEntity>().lambda().eq(StockEntity::getCurPosition, dto.getWcsStation()));
            //如果库存有，那么需要删掉库存
            if (ObjectUtils.isNotEmpty(stockEntities)) {
                for (StockEntity stockEntity : stockEntities) {
                    stockService.deleteById(stockEntity.getId());
                }
            }
        } else if (dto.getStatus() == 2 && !dto.getStationType().equals("ST")) {
            StockEntity entity = stockService.findByCurPosition(dto.getWcsStation());
            //如果库存中没有，那么需要添加库存
            if (ObjectUtils.isEmpty(entity)) {
                StockEntity stockEntity = new StockEntity();
                stockEntity.setContNo(dto.getContNo());
                stockEntity.setCurPosition(dto.getWcsStation());
                stockEntity.setPositionType(dto.getStationType());
                stockEntity.setLockSt(0);
                stockService.insert(stockEntity);
            } else {
                entity.setContNo(dto.getContNo());
                entity.setUpdateDate(new Date());
                stockService.updateById(entity);
            }
        }
        dto.setUpdateDate(new Date());
        this.update(dto);
    }
}
